var ids = [];
var userSelect = [];
var tableDom = $("#tableAjaxId");
$(function () {
    $(".userIdSelect").select2();
    Table.builder()
        .listUrl(WORKSHOP_LIST)
        .createUrl(WORKSHOP_CREATE)
        .updateUrl(WORKSHOP_UPDATE)
        .detailUrl(WORKSHOP_DETAIL)
        .deleteUrl(WORKSHOP_DELETE)
        .customValidate(check)
        .init();

    Request.builder().url(USER_SELECT).isToken(true).callback(function (e) {
        userSelect = e.data;
        if (userSelect.length > 0) {
            Select.builder()
                .dom($("#userId"))
                .value("realName").data(userSelect).initByData();
        }
    }).do();

    function check(validatorDom) {
        if (validatorDom.attr("name") === "name" && validatorDom.val().length > 100) {
            return {
                "retBool": true,
                "msg": "车间名称长度不能超过100个字符"
            };
        }
        if (validatorDom.attr("name") === "manager" && validatorDom.val().length > 100) {
            return {
                "retBool": true,
                "msg": "负责人长度不能超过100个字符"
            };
        }
        if (validatorDom.attr("name") === "remark" && validatorDom.val().length > 512) {
            return {
                "retBool": true,
                "msg": "备注长度不能超过512个字符"
            };
        }
    }


    tableDom.on("click", ".giveMethod", function () {
        var dom = $(this).attr("data-target");
        var id = $(this).attr("dataId")
        document.getElementById("workshopId").value = id;
        Request.builder()
            .url(WORKSHOP_TO_USER_DETAIL + "?workshopId=" + id)
            .isToken(true)
            .callback(function (e) {
                var data = e.data;
                $('#userId').val(data).trigger('change');
            }).get();
        //删除input下方的红字提醒
        if ($(dom).find(".has-error").length > 0) {
            $(dom).find(".has-error").each(function () {
                $(this).removeClass("has-error")
            })
        }
        if ($(dom).find(".help-block").length > 0) {
            $(dom).find(".help-block").each(function () {
                $(this).remove()
            })
        }
    })

    $("#giveAdminUserSubmit").click(function () {
        var data = Form.getValues($("#giveAdminUserForm"));
        if (data.ids.length <= 0) {
            LayerUtil.failMsg("请选择要分配的车间")
            return false;
        }
        Request.builder()
            .url(WORKSHOP_TO_USER)
            .data(data)
            .isToken(true)
            .callback(function (e) {
                if (e.code === "00000") {
                    LayerUtil.successMsg(ERROR_MSG.SUCCESS)
                    setTimeout(function () {
                        tableDom.bootstrapTable('refresh');
                        $("#giveAdminUser").find(".btn-close").trigger("click");
                    }, 1000)
                } else {
                    LayerUtil.failMsg(e.msg != null ? e.msg : ERROR_MSG.FAIL)
                }
            }).post();
    })
});

columns = [
    {
        field: 'name',
        title: '车间名称',
        align: 'center',
        valign: 'middle',
        sortable: true
    }, {
        field: 'user',
        title: '管理员',
        align: 'center',
        valign: 'middle'
    }, {
        field: 'manager',
        title: '负责人',
        align: 'center',
        valign: 'middle'
    }, {
        field: 'totalNum',
        title: '设备数量',
        align: 'center',
        valign: 'middle',
        formatter: function (value, row, index) {
            return "<span style='font-weight: bold;'>" + value + "</span>";
        }
    }, {
        field: 'onlineNum',
        title: '在线数量',
        align: 'center',
        valign: 'middle',
        formatter: function (value, row, index) {
            return "<span style='font-weight: bold;color: #73CDAA;'>" + value + "</span>"
        }
    }, {
        field: 'offlineNum',
        title: '离线数量',
        align: 'center',
        valign: 'middle',
        formatter: function (value, row, index) {
            return "<span style='font-weight: bold;color: #CCCCCC;'>" + value + "</span>"
        }
    }, {
        field: 'lastModifyUser',
        title: '最后修改人',
        align: 'center',
        valign: 'middle'
    }, {
        field: 'lastModifyTime',
        title: '最后修改时间',
        align: 'center',
        valign: 'middle',
        sortable: true,
        formatter: function (value, row, index) {
            return DateUtil.changeDateToString(value, DateFormatEnum.YYYY_MM_DD_HH_MM);
        }
    }, {
        title: "操作",
        align: 'center',
        valign: 'middle',
        width: 250,
        formatter: option
    }
]

function option(value, row, index) {
    return '<a class="btn btn-sm btn-default edits" data-bs-toggle="modal" data-bs-target="#commonDiv" dataId="' + row.id + '" title="编辑" >编辑</a>'
        + ' <a class="btn btn-sm btn-default giveMethod" data-bs-toggle="modal" data-bs-target="#giveAdminUser" dataId="' + row.id + '" title="分配管理员" >分配管理员</a>'
        + ' <a class="btn btn-sm btn-default" href="/admin/station/list.html?workshopId=' + row.id + '"  title="设备" >设备</a>';
}